package com.example.demo.until;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AutoStartUtil {
    public static void enableAutoStart(String appName,String jarPath) throws IOException {
        // 获取计算机标识
        String lowerCase = System.getProperty("os.name").toLowerCase();

        // 判断操作系统
        if (lowerCase.contains("win")) {
            // 自启动地址
            String startupFolder = System.getenv("APPDATA") + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
            Path shortcutPath = Paths.get(startupFolder, appName + ".lnk");

            // 创建快捷方式
            String vbsScript = "Set ws = WScript.CreateObject(\"WScript.Shell\")\n" +
                    "Set shortcut = ws.CreateShortcut(\"" + shortcutPath.toString() + "\")\n" +
                    "shortcut.TargetPath = \"" + jarPath + "\"\n" +
                    "shortcut.WorkingDirectory = \"" + new File(jarPath).getParent() + "\"\n" +
                    "shortcut.Save";

            Path vbsPath = Paths.get(System.getProperty("java.io.tmpdir"), "create_shortcut.vbs");
            Files.write(vbsPath, vbsScript.getBytes());

            System.out.println("输出");
            // 执行VBS脚本
            Runtime.getRuntime().exec("wscript " + vbsPath.toString());
        }
        if (lowerCase.contains("mac")) {
            System.out.println("mac");
        }if (lowerCase.contains("nix") || lowerCase.contains("nux")) {
            System.out.println("linux");
        }
    }
}
